package com.smedi.ismedi.personnel.core.port.adapter.service.impl;

import com.smedi.ismedi.authoritymanagement.api.dto.request.DataPermissionScopeRequest;
import com.smedi.ismedi.authoritymanagement.api.dto.request.RoleAssignmentListQueryRequest;
import com.smedi.ismedi.authoritymanagement.api.dto.request.RoleListQueryRequest;
import com.smedi.ismedi.authoritymanagement.api.dto.response.RoleAssignmentResponse;
import com.smedi.ismedi.authoritymanagement.api.dto.response.RoleBasicResponse;
import com.smedi.ismedi.authoritymanagement.api.dto.response.RoleResponse;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteAuthorityAccessFeignApi;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteAuthorityRoleManagementFeignApi;
import com.smedi.ismedi.personnel.core.domain.aggregatemodel.Role;
import com.smedi.ismedi.personnel.core.domain.aggregatemodel.RoleAssignment;
import com.smedi.ismedi.personnel.core.domain.dto.RoleAssignmentQuery;
import com.smedi.ismedi.personnel.core.domain.dto.RoleQuery;
import com.smedi.ismedi.personnel.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.personnel.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.personnel.core.port.adapter.service.AuthorityAccessRemoteApiService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-8-4 9:25
 */
@Slf4j
@Service
@AllArgsConstructor
public class AuthorityAccessRemoteApiServiceImpl implements AuthorityAccessRemoteApiService {
    private final RemoteAuthorityAccessFeignApi remoteAuthorityAccessFeignApi;
    private final RemoteAuthorityRoleManagementFeignApi remoteAuthorityRoleManagementFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public void boundDataPermissionScope(DataPermissionScopeRequest dataPermissionScopeRequest) {
        ResponseData<Boolean> response = remoteAuthorityAccessFeignApi.putBoundDataPermissionScope(dataPermissionScopeRequest);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public String queryRoleNameByPersonId(Long personId) {
        ResponseData<List<RoleBasicResponse>> roleBasicResponse = remoteAuthorityRoleManagementFeignApi.getQueryRolesByPersonId(personId);
        if (!roleBasicResponse.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, roleBasicResponse.getApplication(), String.valueOf(roleBasicResponse.getErrorMsg()));
        }
        List<String> roleNameList = new ArrayList<>();
        for (RoleBasicResponse i : roleBasicResponse.getData()) {
            roleNameList.add(i.getRoleName());
        }

        String roleNames = StringUtils.join(roleNameList, ",");
        return roleNames;
    }

    @Override
    public List<Long> getQueryAllPersonIdsByRoleId(Long roleId) {
        ResponseData<List<Long>> response = remoteAuthorityRoleManagementFeignApi.getQueryAllPersonIdsByRoleId(roleId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Long> getQueryAllPersonIdsByRoleId(List<Long> roleIds) {
        ResponseData<List<Long>> response = remoteAuthorityRoleManagementFeignApi.getQueryAllPersonIdsByRoleIds(roleIds);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Role> getRoleList(RoleQuery roleQuery) {
        RoleListQueryRequest roleListQueryRequest = adapterConverter.roleQueryToRoleListQueryRequest(roleQuery);
        List<RoleResponse> roleResponses = remoteAuthorityRoleManagementFeignApi.getRoleList(roleListQueryRequest).getData();
        return adapterConverter.roleResponseToRole(roleResponses);
    }

    @Override
    public List<Role> getRoleListWithoutToken(RoleQuery roleQuery) {
        RoleListQueryRequest roleListQueryRequest = adapterConverter.roleQueryToRoleListQueryRequest(roleQuery);
        List<RoleResponse> roleResponses = remoteAuthorityRoleManagementFeignApi.getRoleListWithoutToken(roleListQueryRequest).getData();
        return adapterConverter.roleResponseToRole(roleResponses);
    }

    @Override
    public List<RoleAssignment> getRoleAssignmentList(RoleAssignmentQuery roleAssignmentQuery) {
        RoleAssignmentListQueryRequest roleListQueryRequest = adapterConverter.roleAssignmentQueryToRoleAssignmentListQueryRequest(roleAssignmentQuery);
        List<RoleAssignmentResponse> roleAssignmentResponses = remoteAuthorityRoleManagementFeignApi.getRoleAssignmentList(roleListQueryRequest).getData();
        return adapterConverter.roleAssignmentResponseToRoleAssignment(roleAssignmentResponses);
    }

    @Override
    public List<RoleAssignment> getRoleAssignmentListWithoutToken(RoleAssignmentQuery roleAssignmentQuery) {
        RoleAssignmentListQueryRequest roleListQueryRequest = adapterConverter.roleAssignmentQueryToRoleAssignmentListQueryRequest(roleAssignmentQuery);
        List<RoleAssignmentResponse> roleAssignmentResponses = remoteAuthorityRoleManagementFeignApi.getRoleAssignmentListWithoutToken(roleListQueryRequest).getData();
        return adapterConverter.roleAssignmentResponseToRoleAssignment(roleAssignmentResponses);
    }
}
